package com.my.supplier.controller;

import com.my.common.JsonUtil;
import com.my.supplier.entity.Supplier;
import com.my.supplier.service.SupplierService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin(origins="*")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    
    //查询供应商
    @RequestMapping("getSupplicer")
    public String getSupplicer(String supName,String supTel,String supCoding){
    	System.out.println(supName);
    	System.out.println(supTel);
    	System.out.println(supCoding);
        Map<String, Object> map=new HashMap<String,Object>();
        map.put("supName", supName);
        map.put("supTel", supTel);
        map.put("supCoding", supCoding);
        return JsonUtil.success(supplierService.getSupplier(map));
    }
    
    //添加供应商
    @RequestMapping("addSupplicer")
    public String addSupplicer(Supplier supplier){
    	System.out.println("supplier");
        supplierService.addSupplier(supplier);
        return JsonUtil.success();
    }
    
    //修改供应商
    
    @RequestMapping("updateSupplicer")
    public String updateSupplicer(Supplier supplier){
    	System.out.println(supplier);
        supplierService.updateSupplicer(supplier);
        return JsonUtil.success();
    }
    
  //供应商的修改回显
    @RequestMapping("getSupplierById")
    public String getSupplierById(String id){
    	
    	Supplier supp=supplierService.getSupplierById(Long.valueOf(id));
        return JsonUtil.success(supp);
    }
    
    //文件上传
    @RequestMapping("form")
    public String handleFormUpload(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file) throws IOException {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            System.out.println(bytes+"================================================");
            // store the bytes somewhere
            return "redirect:uploadSuccess";
        }

        return "redirect:uploadFailure";
    }
    
}
